<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="部门名称" prop="deptName">
        <el-input
          v-model="queryParams.deptName"
          placeholder="请输入部门名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd()"
          >新增</el-button
        >
      </el-col>
      <!-- <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getDeptTree"
      ></right-toolbar> -->
    </el-row>

    <el-table
      v-loading="loading"
      :data="menuList"
      row-key="id"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="deptName" label="部门名称"></el-table-column>
      <el-table-column prop="orderNum" label="排序"></el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="200"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            >新增</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改部门对话框 -->
    <form-dialog ref="form" @submit_success="getDeptTree"></form-dialog>
  </div>
</template>

<script>
import { getDeptTree, remove } from "@/api/sys/dept";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import sysMixins from "@/mixins/sysMixins";
import { selectDictLabel, initForm, getParentName } from "./options";
import FormDialog from "./form";

export default {
  name: "Menu",
  mixins: [sysMixins],
  components: { FormDialog },
  data() {
    return {
      // 显示搜索条件
      showSearch: true,
      // 部门表格树数据
      menuList: [],
      // 部门树选项
      menuOptions: [],
      // 显示状态数据字典
      visibleOptions: [],
      // 部门状态数据字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        menuName: undefined,
        visible: undefined,
      },
      form: {},
    };
  },
  created() {
    this.getDeptTree();
  },
  methods: {
    /** 查询部门列表 */
    getDeptTree() {
      this.loading = true;
      getDeptTree(this.queryParams).then(({ data }) => {
        this.menuList = data;
        this.loading = false;
      });
    },
    // 部门状态字典翻译
    statusFormat(row) {
      if (row.menuType == "F") {
        return "";
      }
      return selectDictLabel(this.statusOptions, row.status);
    },
    // 表单重置
    reset() {
      this.form = initForm();
      this.resetForm("form");
    },
    resetForm(refName) {
      if (this.$refs[refName]) {
        this.$refs[refName].resetFields();
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getDeptTree();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleAdd(row) {
      let form = {};
      if (row != null && row.id) {
        form = {
          parentId: row.id,
          parentName: row.deptName,
        };
      } else {
        form = {
          parentId: -1,
          parentName: "顶部部门",
        };
      }
      this.$refs.form.open("add", form);
    },
    handleUpdate(row) {
      let form = row;
      form.parentName = getParentName(row.parentId, this.menuList);
      this.$refs.form.open("edit", form);
    },
    handleDelete(row) {
      this.$confirm('是否确认删除名称为"' + row.deptName + '"的部门?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function() {
          return remove(row.id);
        })
        .then(() => {
          this.getDeptTree();
          this.$message.success("删除成功");
        })
        .catch((error) => {
          this.$message.error(error.msg);
        });
    },
  },
};
</script>
